package ysq.xyg.wxapi.dao.impl;

import com.baidu.aip.ocr.AipOcr;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;
import ysq.xyg.base.util.AipSingleton;
import ysq.xyg.web.model.po.WebAccount;
import ysq.xyg.wxapi.dao.WxDao;
import ysq.xyg.wxapi.model.base.MsgNews;
import ysq.xyg.wxapi.model.base.MsgRequest;
import ysq.xyg.wxapi.model.base.MsgText;
import ysq.xyg.wxapi.util.MsgXmlUtil;
import ysq.xyg.wxapi.util.WxMessageBuilder;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/6/23 0023.
 */
@Repository("wxDao")
public class WxDaoImpl implements WxDao {
    AipOcr client = AipSingleton.getAipOcrInstance();
    /**
     * 文本消息处理
     * */
    @Override
    public String msgResponseText(MsgRequest msgRequest, WebAccount webAccount){
        String content = msgRequest.getContent();
        if(!StringUtils.isEmpty(content)){//文本消息
            String tmpContent = content.trim();
            if ("1".equals(tmpContent)) {
                MsgText msgText = new MsgText();
                msgText.setContent("ABCD3F");
                return MsgXmlUtil.textToXml(WxMessageBuilder.getMsgResponseText(msgRequest, msgText));
            }
            else if ("2".equals(tmpContent)) {
                MsgText msgText = new MsgText();
                msgText.setContent("我好");
                return MsgXmlUtil.textToXml(WxMessageBuilder.getMsgResponseText(msgRequest, msgText));
            }
            else if ("3".equals(tmpContent)) {
                List<MsgNews> msgNewsList=new ArrayList<MsgNews>();
                return MsgXmlUtil.newsToXml(WxMessageBuilder.getMsgResponseNews(msgRequest, msgNewsList));
            }else {
                MsgText msgText = new MsgText();
                msgText.setContent("发送错误");
                return MsgXmlUtil.textToXml(WxMessageBuilder.getMsgResponseText(msgRequest, msgText));
            }
        }
        return null;
    }
    /**
     * 图片消息处理
     * */
    @Override
    public String msgResponseImage(MsgRequest msgRequest, WebAccount webAccount){
        MsgText msgText = new MsgText();
        msgText.setContent(AipSingleton.acceptText(client,msgRequest.getPicUrl()).toString());
        return MsgXmlUtil.textToXml(WxMessageBuilder.getMsgResponseText(msgRequest, msgText));
    }

}
